<template>
	<view class="pmain">
		<view v-if="depth!=null">
			<view class="item">
				<text class="price">价格</text>
				<text class="num">数量</text>
			</view>
			<view class="item uni-down" v-for="(item,index) in depth.asks" :key="index">
				<text class="price">{{item[0]}}</text>
				<text class="num">{{item[1]}}</text>
				<!-- <view class="bg buybg" style="width: 50%;"></view> -->
			</view>
			<view class="current-price" v-if="ticker!=null">
				<view class="uni-h5 uni-bold uni-up usdt" v-if="ticker.last>preTicker.last">{{ticker.last}}</view>
				<view class="uni-h5 uni-bold uni-down usdt" v-else>{{ticker.last}}</view>
				<!-- <view class="uni-h7 cny uni-text-gray">≈28767.23 CNY</view> -->
			</view>
			<view class="item uni-up" v-for="(item,index2) in depth.bids" :key="item[0]">
				<text class="price">{{item[0]}}</text>
				<text class="num">{{item[1]}}</text>
				<!-- <view class="bg sellbg" style="width: 80%;"></view> -->
			</view>
		</view>
		<!-- <view class="depth uni-thinner-border " @click="depthClick">
			<view class="uni-h6">深度{{depthValue}}</view>
			<view class="popover-wrap" v-show="popoverVisible">
				<view class="kailong"></view>
				<view class="popover uni-thinner-border">
					<view class="item uni-divider" @click="depthChange(1)">1</view>
					<view class="item uni-divider" @click="depthChange(2)">2</view>
					<view class="item uni-divider" @click="depthChange(3)">3</view>
					<view class="item uni-divider" @click="depthChange(4)">4</view>
				</view>
			</view>
		</view> -->

	</view>
</template>

<script>	
	export default {
		data: function() {
			return {				
				popoverVisible: false,
				depthValue: 1,
				ticker: null,
				preTicker: null,
				item: Object,
				depth: null,
				freshTimer:null
			}
		},
		mounted() {		
			this.fresh()
			uni.$on("spotPositionFresh",this.fresh)			
		},

		computed: {},
		methods: {
			depthClick(e) {
				this.popoverVisible = !this.popoverVisible;
			},
			depthChange(e) {
				this.depthValue = e;
				this.$emit('depthChange', e);				
			},

			fresh:function() {
				console.log("fresh ticker depth in position")
				this.requestTicker()
				this.requestDepth()
			},

			requestTicker() {
				uni.request({
					url: this.api + '/apis/okex/unauth?simpleUrl=/api/spot/v3/instruments/' + this.getDefaultSpot().instrumentId +
						'/ticker',
					method: 'GET',
					success: res => {
						console.log('getticker resp:')
						console.log(res.data.data)
						let tempTicker = res.data.data
						if (this.ticker == null) {
							this.ticker = tempTicker							
						}
						if (this.preTicker == null) {
							this.preTicker = tempTicker							
						}
						if (this.ticker.last != tempTicker.last) {
							this.preTicker = this.ticker						
							this.ticker = tempTicker							
						}
					}
				});

			},
			requestDepth() {
				console.log("get depth")
				uni.request({
					url: this.api + '/apis/okex/unauth?simpleUrl=/api/spot/v3/instruments/' + this.getDefaultSpot().instrumentId +
						'/book?size=5&depth=0.1',
					method: 'GET',
					success: res => {
						console.log('getDepth resps:')
						console.log(res.data.data)
						let tempDepth = res.data.data
						// tempDepth.asks.reverse()
						this.depth = tempDepth						
					}
				});
			}
		}
	}
</script>

<style scoped>
	.pmain {
		width: 100%;
		height: 600upx;
		position: relative;
		justify-content: space-between;
		text-align: left;
	}

	.pmain .item {
		position: relative;
		width: 100%;
		display: flex;
		font-size: 24upx;
	}

	.pmain .item .bg {
		height: 100%;
		position: absolute;
		top: 0;
		right: 0;
		z-index: -9999;
	}

	.pmain .item .buybg {
		background: #fdf4f7;
	}

	.pmain .item .sellbg {
		background: #f0faf9;
	}

	.pmain .item .price {
		display: block;
		width: 50%;
		text-align: left;
	}

	.position .item .num {
		display: block;
		width: 50%;
		text-align: right;
		padding-right: 10upx;
	}

	.pmain .current-price {
		margin: 10upx 0;
	}

	.pmain .current-price .usdt {}

	.pmain .depth {
		position: relative;
		margin-top: 10upx;
		margin-right: 10upx;
	}

	.pmain .depth .uni-h6 {
		text-align: center;
		height: 60upx;
		line-height: 60upx;
	}

	.pmain .depth .popover {
		position: absolute;
		top: 80upx;
		width: 100%;
		z-index: 9999999999;
		background: #ffffff;
		display: inline-block;
		border-radius: 20px;
	}

	.pmain .depth .popover .item {
		width: 100%;
		height: 60upx;
		line-height: 60upx;
		display: block;
		text-align: center;
	}

	.pmain .depth .kailong {
		position: absolute;
		left: 50%;
		top: 74upx;
		transform: translate(-50%, -50%);
		max-width: 50%;
		width: 0;
		height: 0;
		border-right: 20px solid transparent;
		border-left: 20px solid transparent;
		border-bottom: 20px solid #E0E0E0;
	}

	.popover-wrap .uni-thinner-border:before {
		border-radius: 15px !important;
	}
</style>
